<template>
  <div class="app-container">
    <div class="head-container">
      <div v-if="searchToggle">
        <el-input v-model="query.keyword" clearable size="small" placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter="fetchList" />
        <rrOperation />
      </div>
      <crudOperation>
        <template v-slot:left>
        <el-button
          class="filter-item"
          type="danger"
          icon="delete"
          size="small"
          :loading="delLoading"
          :disabled="selections.length === 0"
          @click="doDelete(selections)"
        >
          强退
        </el-button>
        </template>
      </crudOperation>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="tableLoading" :data="tableData" style="width: 100%;" @selection-change="selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="username" label="账号" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="ip" label="登录IP" />
      <el-table-column :show-overflow-tooltip="true" prop="address" label="登录地点" />
      <el-table-column prop="browser" label="浏览器" />
      <el-table-column prop="loginTime" label="登录时间">
        <template v-slot="scope">
          <span>{{ scope.row.loginTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="70px" fixed="right">
        <template v-slot="scope">
          <div v-permission="['onlineuser:kickout']">
            <el-popconfirm
                confirmButtonText='确定'
                cancelButtonText='取消'
                icon="warning"
                iconColor="red"
                title="确定强制退出该用户吗？"
                @confirm="delMethod(scope.row.key, scope.$index)"
            >
              <template #reference>
                <el-button size="small" type="text">强退</el-button>
              </template>
            </el-popconfirm>
          </div>

        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from "@crud/Pagination"
import useCrud from "@crud/useCrud";
import {list, remove} from '@/api/system/online'

export default {
  name: 'OnlineUser',
  components: { pagination, crudOperation, rrOperation },
  setup() {
    return {
      delLoading: false,
      ...useCrud({
        pageSize: 10,
        title: "在线用户",
        methods: {list, remove},
        optShow: {
          create: false,
          edit: false,
          remove: false,
          download: false,
          reset: true
        }
      })
    }
  },

  methods: {
    doDelete(datas) {
      this.$confirm(`确认强退选中的${datas.length}个用户?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delMethod(datas)
      }).catch(() => {})
    },
    // 踢出用户
    delMethod(key) {
      const ids = []
      if (key instanceof Array) {
        key.forEach(val => {
          ids.push(val.key)
        })
      } else ids.push(key)
      this.delLoading = true
      remove(ids).then(() => {
        this.delLoading = false
        this.$notify.success({
          message: '强退成功',
          duration: 2500
        })
        this.fetchList()
      }).catch(() => {
        this.delLoading = false
      })
    }
  }
}
</script>
